<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mobile Mashup 2011</title>
<link rel="stylesheet" type="text/css" href="jquery/jquery.mobile-1.0a4.1.css" />
<script src="jquery/jquery-1.6.1.min.js"></script>
<script src="jquery/jquery.mobile-1.0a4.1.js"></script>
<script type="text/javascript">
	var fb_ids = [];
	
	$('document').ready(function() {
		$('#user_id').focus();
	});
	
	function doSubmit() {
		var userId = $('#user_id').val();
		$.post("login_check.php", {user_id: userId, passwd: $('#passwd').val()}, function(data) {
			if (data.status == 0) {
				navigator.geolocation.getCurrentPosition(function(pos) {
					$.post("session.php", {user_id: userId, lat: pos.coords.latitude, lng: pos.coords.longitude}, function(result) {
					}, "json");
				});
				$.getJSON("get_preset.php", {user_id: userId}, function(data) {
					sessionStorage['user_id'] = userId;
					sessionStorage['bus_numbers'] = data.bus_numbers;
					sessionStorage['facebook_id'] = data.facebook_id;
					sessionStorage['crime_rate'] = data.crime_rate;
					sessionStorage['isBusNoticed'] = 'false';
					
					// 관심사 저장하기
					$.getJSON("get_interest_in.php", {facebook_id: data.facebook_id}, function(jdata) {
						sessionStorage['interest'] = jdata.interest;
						//alert(sessionStorage['interest']);
					});

					// 친구목록도 저장하기
					$.getJSON("get_fb_friends.php", {user_id: userId}, function(jdata) {
						$.each(jdata, function(friends, friend) {
							fb_ids.push(friend.facebook_id);
						});
						
						sessionStorage["fb_friend_ids"] = fb_ids;
						console.log(sessionStorage["fb_friend_ids"]);
					});					
					
					//console.log(sessionStorage['user_id'] + " " + sessionStorage['bus_numbers'] + " " + sessionStorage['crime_rate']);
					location.href = "context_monitor.html";
				});
			}
		}, "json");
	}
</script>
</head>
<body>
<div data-role="page" data-theme="b">
	<div data-role="header">
		<h1>Mobile Mashup 2011</h1>
	</div>
	<div data-role="content" data-theme="c">
		<form data-ajax="false" action="/" method="post">
			<div data-role="fieldcontain">
				<label for="user_id">ID:</label>
				<input type="text" name="user_id" id="user_id" value=""  />
				<label for="passwd">Password:</label>
				<input type="password" name="passwd" id="passwd" value=""  />
			</div>			
			<input type="button" value="Login" onclick="doSubmit();" />
		</form>
	</div>
</div>
</body>
</html>